package com.chineseall.rebirth.bridge.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jmind.core.security.MD5;
import jmind.core.support.HttpUtil;
import jmind.core.support.LogUtil;
import jmind.core.util.GlobalConstants;
import jmind.core.util.RunnableTaskExecutor;

/**
 * 内部http 接口
 * @author wbxie
 * 2013-11-27
 */
public class InteriorHttpApi {

    private static final String pay_url = "http://www.17k.com/presentation.action";

    public static final String key = "s5jHj6YujIuTrF1DcNm8opfjenr2dmUd";

    public static void main(String[] args) {
        doPay("激活包月", "{'by':" + 12 + "}", 34892642);
        // InteriorHttpApi.doPay("360免费7天看书", "{'br':" + 7 + "}", 32000046);
    }

    /**
     * 免费激活注册送包月
     * 2013-11-27 
     * @param userId
     */
    public static void freeTry(final int userId) {
        doPay("免费体验1月", "{'by':" + 1 + "}", userId);
    }

    /**
     *  包月接口
     * 2013-11-27 
     * @param description
     * @param content
     * @param userId
     */
    public static void doPay(final String description, final String content, final Integer userId) {
        RunnableTaskExecutor.getInstance().runSimpleTask(new Runnable() {
            @Override
            public void run() {
                long time = System.currentTimeMillis();
                String MD5Content = description + "|" + content + "|" + userId + "|" + time + "|" + key;
                String hmac = MD5.md5(MD5Content);
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("description", description);
                m.put("content", content);
                m.put("userId", userId);
                m.put("time", time);
                m.put("hmac", hmac);
                try {
                    String result = HttpUtil.post(pay_url, m, GlobalConstants.ENCODE);
                    //    System.out.println("result=" + result);
                    LogUtil.debug("setfreeTry userid={},result={}", userId, result);
                    //            JSONObject json = JSONObject.parseObject(result);
                    //            Integer status = json.getInteger("status");
                    //            System.out.println(json);
                    //            if (status != 0) {
                    //                String msg = json.getString("msg");
                    //                throw new RuntimeException(msg);
                    //            }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    /**
     * 客户端登录 增加登录任务
     * 2013-11-26 
     * @param uid
     */
    public static void appLoginTask(final int uid) {
        RunnableTaskExecutor.getInstance().runSimpleTask(new Runnable() {
            @Override
            public void run() {
                doTask(uid, 6);

            }
        });
    }

    public static void doTask(int uid, int taskId) {
        //任务系统
        Map<String, Object> m = new HashMap<String, Object>();
        try {
            long time = System.currentTimeMillis();
            String MD5Content = uid + "|" + taskId + "|" + time + "|" + key;
            String hmac = MD5.md5(MD5Content);
            m.put("userId", uid);
            m.put("taskId", taskId);
            m.put("time", time);
            m.put("hmac", hmac);
            HttpUtil.post("http://www.17k.com/task/saveTaskDetail.action", m, GlobalConstants.ENCODE);
        } catch (Exception e) {
            LogUtil.debug("doTask map={}", m);
        }

    }

}
